热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python正则表达式学习记录及常用方法

本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。

在这里插入图片描述在这里插入图片描述学习记录,防止遗忘
在这里插入图片描述
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

在这里插入图片描述
以上方法显的非常繁琐。。。

re 模块使 Python 语言拥有全部的正则表达式功能。

re.search 扫描整个字符串,匹配成功re.search方法返回一个匹配的对象,否则返回None。

re.search(pattern, string, flags=0)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
The ’r‘ at the beginning of the pattern indicates that this is a rawstring. This means that Python interpreter shouldn’t try to interpret any special characters, and instead, should just pass the string to the function as is. In this example, there are no special characters. The rawstring and the normal string are exactly the same, but it’s a good idea to always use rawstrings for regular expressions in Python.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
We can also pass additional options to the search function. For example, if we want our match to be case insensitive, we can do this by passing the re.IGNORECASE option.

We’ve seen by now how we can use a dot in our regular expressions as a special character that can match any character. In the regex world, this is known as a wildcard because it can match more than one character.
在这里插入图片描述
Sometimes we may want to match any characters that aren’t in a group. To do that, we use a circumflex inside the square brackets. For example, let’s create a search pattern that looks for any characters that’s not a letter.
在这里插入图片描述
If we want to match either one expression or another, we can use the pipe symbol to do that. This lets us list alternative options that can get matched. For example, we could have an expression that matches either the word cat or the word dog, like this.
在这里插入图片描述
RegEx concept, repeated matches. It’s quite common to see expressions that include a dot followed by a star. This means that it matches any character repeated as many times as possible including zero.
re* 匹配0个或多个的表达式
在这里插入图片描述
the Star takes as many characters as possible. In programming terms, we say that this behavior is greedy. It’s possible to modify the repetition qualifiers to make them less greedy. But we won’t get into that now.

Other implementations like the one used by Python or by the Egrep command include two additional repetition qualifiers plus and question mark, that can help us construct more complex expressions. Let’s check that out. The plus character matches one or more occurrences of the character that comes before it. So we had the pattern O plus L plus. Let’s check it against a few words.
In this case, there was one occurrence of each. In the match pattern shows us the shortest possible matching string.
re+ 匹配1个或多个的表达式
在这里插入图片描述
The question mark symbol is yet another multiplier. It means either zero or one occurrence of the character before it.
re? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
在这里插入图片描述
special characters:dot, star, plus, question mark, circumflex, dollar sign, and square brackets在这里插入图片描述
To match an actual dot, we need to use an Escape character, which in the case of regular expressions is a backslash character. So let’s add that to our pattern.在这里插入图片描述
It can get really confusing with backslashes since they’re also used to define some special string characters. We’ve called out, for example, that \n is a sequence using Python to indicate a new line, and \t does the same for tabs. When we see a pattern that includes a backslash, it could be escaping a special regex character or a special string character.
在这里插入图片描述
Using rawstrings, like we’ve been doing, helps avoid some of these possible confusion because the special characters won’t be interpreted when generating the string. They will only be interpreted when parsing the regular expression. On top of this, Python also uses the backslash for a few special sequences that we can use to represent predefined sets of characters. For example, \w matches any alphanumeric character including letters, numbers, and underscores. Let’s check out a couple of examples.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
A great resource for testing out your regular expressions is a website called regex101.com. You can use this to try out your regexes, analyze each part of the expression, and figure out what’s up with them when they don’t work.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Python also offers numeric repetition qualifiers. These are written between curly brackets and can be one or two numbers specifying a range. For example, to match any string of exactly five letters, we can use an expression like this one:
在这里插入图片描述
Now we have an extra match for the word that’s actually longer. What if we wanted to match all the words that are exactly five letters long? We can do that using \b, which matches word limits at the beginning and end of the pattern, to indicate that we want full words, like this:
We can also have two numbers in the range. For example, if we wanted to match a range of five to ten letters or numbers, we could use an expression like this one:
在这里插入图片描述
在这里插入图片描述
Splitting and Replacing
在这里插入图片描述
Use a regular expression for searching in a plain string for replacing.
在这里插入图片描述
Use regular expressions for the replacing.
在这里插入图片描述
So once again we’d use parentheses to create capturing groups. In the first parameter, we’ve got an expression that contains the two groups that we want to match: one before the comma and one after the comma. We want to use a second parameter to replace the matching string. We use backslash two to indicate the second captured group followed by a space and backslash one to indicate the first captured group. When referring to captured groups, a backslash followed by a number indicates the corresponding captured group. This is a general notation for regular expressions, and it’s used by many tools that support regexes, not just Python.

Bonus:Regex Cross­word


推荐阅读
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 本文详细介绍了Python中正则表达式和re模块的使用方法。首先解释了转义符的作用,以及如何在字符串中包含特殊字符。然后介绍了re模块的功能和常用方法。通过学习本文,读者可以掌握正则表达式的基本概念和使用技巧,进一步提高Python编程能力。 ... [详细]
  • 恶意软件分析的最佳编程语言及其应用
    本文介绍了学习恶意软件分析和逆向工程领域时最适合的编程语言,并重点讨论了Python的优点。Python是一种解释型、多用途的语言,具有可读性高、可快速开发、易于学习的特点。作者分享了在本地恶意软件分析中使用Python的经验,包括快速复制恶意软件组件以更好地理解其工作。此外,作者还提到了Python的跨平台优势,使得在不同操作系统上运行代码变得更加方便。 ... [详细]
  • Python操作MySQL(pymysql模块)详解及示例代码
    本文介绍了使用Python操作MySQL数据库的方法,详细讲解了pymysql模块的安装和连接MySQL数据库的步骤,并提供了示例代码。内容涵盖了创建表、插入数据、查询数据等操作,帮助读者快速掌握Python操作MySQL的技巧。 ... [详细]
  • Python使用Pillow包生成验证码图片的方法
    本文介绍了使用Python中的Pillow包生成验证码图片的方法。通过随机生成数字和符号,并添加干扰象素,生成一幅验证码图片。需要配置好Python环境,并安装Pillow库。代码实现包括导入Pillow包和随机模块,定义随机生成字母、数字和字体颜色的函数。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
author-avatar
myq9395014
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有